<?php

namespace app\modules\admin\controllers;

use Yii;
use app\modules\admin\models\CwPanel;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;


/**
 * CwPanelController implements the CRUD actions for CwPanel model.
 */
class CwPanelController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all CwPanel models.
     * @return mixed
     */
    public function actionIndex()
    {
        $this->RBAC();
        $dataProvider = new ActiveDataProvider([
            'query' => CwPanel::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single CwPanel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $this->RBAC();
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new CwPanel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $this->RBAC();
        $model = new CwPanel();

        if (\Yii::$app->request->isPost){            
            $model->pan_pic_url = UploadedFile::getInstance($model, "pan_pic_url");
            $url = 'panel/'. time().'.'.$model->pan_pic_url->extension;
            $model->pan_pic_url->saveAs($url);
            $model->pan_pic_url = $url;
            $post = Yii::$app->request->post();
            $post['CwPanel']['pan_pic_url'] = $url;
            if ($model->load($post) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->pan_id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        }else{
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->pan_id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Updates an existing CwPanel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $this->RBAC();
        $model = $this->findModel($id);

        if (\Yii::$app->request->isPost){            
            $model->pan_pic_url = UploadedFile::getInstance($model, "pan_pic_url");
            $url = 'panel/'. time().'.'.$model->pan_pic_url->extension;
            $model->pan_pic_url->saveAs($url);
            $model->pan_pic_url = $url;
            $post = Yii::$app->request->post();
            $post['CwPanel']['pan_pic_url'] = $url;
            if ($model->load($post) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->pan_id]);
            } else {
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        }else{
            if ($model->load(\Yii::$app->request->post()) && $model->save()) {
                return $this->redirect(['view', 'id' => $model->pan_id]);
            } else {
                return $this->render('update', [
                    'model' => $model,
                ]);
            }
        }
    }

    /**
     * Deletes an existing CwPanel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->RBAC();
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the CwPanel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return CwPanel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        $this->RBAC();
        if (($model = CwPanel::findOne($id)) !== null) {
            $model->pan_pic_url = \Yii::$app->urlManager->hostInfo.\Yii::$app->urlManager->baseUrl."/".$model->pan_pic_url;
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function RBAC(){
        if (\Yii::$app->session["admin"]["isLogin"] != "868"){
            return $this->redirect(['default/index']);
        }
    }
}
